package com.cyt.web.controller.work.antiepidemic;

import cn.hutool.core.collection.CollUtil;
import com.cyt.base.enums.BusinessType;
import com.cyt.base.enums.OperatorType;
import com.cyt.base.model.dto.ReturnResult;
import com.cyt.base.util.ProProperties;
import com.cyt.base.util.QrCodeUtil;
import com.cyt.base.util.StringUtils;
import com.cyt.sys.aspect.BaseLog;
import com.cyt.web.controller.base.BaseController;
import com.cyt.work.antiepidemic.model.po.AntiepidemicBuilding;
import com.cyt.work.antiepidemic.model.po.AntiepidemicCommunity;
import com.cyt.work.antiepidemic.model.vo.AntiepidemicCommunityVo;
import com.cyt.work.antiepidemic.model.vo.AntiepidemicSelectVo;
import com.cyt.work.antiepidemic.service.AntiepidemicBuildingService;
import com.cyt.work.antiepidemic.service.AntiepidemicCommunityService;
import org.iherus.codegen.qrcode.SimpleQrcodeGenerator;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * e门通-社区信息控制层
 *
 * @author rong yuan
 * @date 2020-2-12 1:46:59
 */
@RestController
public class AntiepidemicCommunityController extends BaseController {

    @Resource
    private AntiepidemicCommunityService antiepidemicCommunityService;
    @Resource
    private AntiepidemicBuildingService antiepidemicBuildingService;

    /**
     * 统一跳转
     *
     * @param model 数据模型
     * @return Model
     * @author rong yuan
     * @date 2020-2-12 1:46:59
     */
    @RequestMapping("/work/antiepidemic/antiepidemiccommunity/*")
    public Model goPage(Model model, String applyId) {
        return model.addAttribute("applyId", applyId);
    }

    /**
     * 分页查询
     *
     * @param antiepidemicCommunityVo 接受参数对象
     * @param page                    当前页面
     * @param limit                   页面数量
     * @return ReturnResult
     * @author rong yuan
     * @date 2020-2-12 1:46:59
     */
    @BaseLog(module = "e门通", classz = "社区信息", methods = "分页查询对象", businessType = BusinessType.GET, operatorType = OperatorType.MANAGE)
    @RequestMapping(value = "/work/antiepidemic/antiepidemiccommunity/page", method = RequestMethod.POST)
    public ReturnResult page(AntiepidemicCommunityVo antiepidemicCommunityVo, int page, int limit) {
        return antiepidemicCommunityService.page(antiepidemicCommunityVo, page, limit);
    }

    /**
     * 跳转添加或编辑界面
     *
     * @param id 主键
     * @return ReturnResult
     * @author rong yuan
     * @date 2020-2-12 1:46:59
     */
    @BaseLog(module = "e门通", classz = "社区信息", methods = "跳转添加或编辑界面", businessType = BusinessType.GET, operatorType = OperatorType.MANAGE)
    @RequestMapping(value = "/work/antiepidemic/antiepidemiccommunity/getEdit", method = RequestMethod.GET)
    public ModelAndView getEdit(String id, String applyId) {
        ModelAndView view = new ModelAndView("/work/antiepidemic/antiepidemiccommunity/antiepidemicCommunityEdit");
        if (StringUtils.isNotBlank(id)) {
            AntiepidemicCommunity antiepidemicCommunity = antiepidemicCommunityService.selectByPrimaryKey(id);
            String maxTripTimes = antiepidemicCommunity.getMaxTripTimes();
            String[] split = maxTripTimes.split("/");
            view.addObject("dayTripTimes", split[0]);
            view.addObject("frequencyTripTimes", split[1]);
            view.addObject("antiepidemicCommunity", antiepidemicCommunity);
        }
        view.addObject("applyId",applyId);
        return view;
    }

    /**
     * 保存或更新
     *
     * @param request                 请求对象
     * @param antiepidemicCommunityVo 接受参数对象
     * @return ReturnResult
     * @author rong yuan
     * @date 2020-2-12 1:46:59
     */
    @BaseLog(module = "e门通", classz = "社区信息", methods = "保存或更新对象", businessType = BusinessType.INSERT, operatorType = OperatorType.MANAGE)
    @RequestMapping(value = "/work/antiepidemic/antiepidemiccommunity/addOrUpdate", method = RequestMethod.POST)
    public ReturnResult addOrUpdate(HttpServletRequest request, AntiepidemicCommunityVo antiepidemicCommunityVo) {
        return antiepidemicCommunityService.addOrUpdate(antiepidemicCommunityVo, acceptCurrentUser(request));
    }

    /**
     * 根据条件逻辑删除
     *
     * @param request                 请求对象
     * @param antiepidemicCommunityVo 接受参数对象
     * @return ReturnResult
     * @author rong yuan
     * @date 2020-2-12 1:46:59
     */
    @BaseLog(module = "e门通", classz = "社区信息", methods = "逻辑删除对象", businessType = BusinessType.DELETE, operatorType = OperatorType.MANAGE)
    @RequestMapping(value = "/work/antiepidemic/antiepidemiccommunity/remove", method = RequestMethod.GET)
    public ReturnResult remove(HttpServletRequest request, AntiepidemicCommunityVo antiepidemicCommunityVo) {
        return antiepidemicCommunityService.remove(antiepidemicCommunityVo, acceptCurrentUser(request));
    }

    /**
     * 根据条件物理删除
     *
     * @param request                 请求对象
     * @param antiepidemicCommunityVo 接受参数对象
     * @return ReturnResult
     * @author rong yuan
     * @date 2020-2-12 1:46:59
     */
    @BaseLog(module = "e门通", classz = "社区信息", methods = "物理对象对象", businessType = BusinessType.DELETE, operatorType = OperatorType.MANAGE)
    @RequestMapping(value = "/work/antiepidemic/antiepidemiccommunity/delete", method = RequestMethod.GET)
    public ReturnResult delete(HttpServletRequest request, AntiepidemicCommunityVo antiepidemicCommunityVo) {
        return antiepidemicCommunityService.delete(antiepidemicCommunityVo, acceptCurrentUser(request));
    }

    @RequestMapping(value = "/work/antiepidemic/antiepidemiccommunity/cascadeAcceptSelect", method = RequestMethod.POST)
    public ReturnResult cascadeAcceptSelect(String applyId) {
        // 级联数据
        AntiepidemicCommunityVo antiepidemicCommunityVo = new AntiepidemicCommunityVo();
        antiepidemicCommunityVo.setApplyId(applyId);
        List<AntiepidemicCommunity> list = antiepidemicCommunityService.findList(antiepidemicCommunityVo);
        List<AntiepidemicSelectVo> resultList = new ArrayList<>();
        if (CollUtil.isNotEmpty(list)) {
            for (AntiepidemicCommunity antiepidemicCommunity : list) {
                AntiepidemicSelectVo antiepidemicSelectVo = new AntiepidemicSelectVo();
                antiepidemicSelectVo.setId(antiepidemicCommunity.getId());
                antiepidemicSelectVo.setName(antiepidemicCommunity.getBuildingName());
                AntiepidemicBuilding antiepidemicBuildingSearch = new AntiepidemicBuilding();
                antiepidemicBuildingSearch.setCommunityId(antiepidemicCommunity.getId());
                List<AntiepidemicSelectVo> select = antiepidemicBuildingService.selectAntiepidemicSelectVoList(antiepidemicBuildingSearch);
                antiepidemicSelectVo.setChildren(select);
                resultList.add(antiepidemicSelectVo);
            }
        }
        return new ReturnResult(resultList);
    }

    @RequestMapping(value = "/work/antiepidemic/antiepidemiccommunity/acceptSelect", method = RequestMethod.POST)
    public ReturnResult acceptSelect(String dictType) {
        return antiepidemicCommunityService.acceptSelect(dictType);
    }

    @RequestMapping("/shareQrCode")
    public void shareQrCode(HttpServletResponse response, String applyId, String communityId) throws IOException {
        String url = ProProperties.getInstance().getBaseUrl() + "/weChat/oauth?applyId=" + applyId + "&path=/phone/antiepidemic/index&pathParams=communityId*" + communityId;
        // 需要
        AntiepidemicCommunity antiepidemicCommunity = antiepidemicCommunityService.selectByPrimaryKey(communityId);
        QrCodeUtil code = new QrCodeUtil();
        QrCodeUtil.CodeModel codeModel = code.new CodeModel();
        codeModel.setContents(url);
        codeModel.setWidth(400);
        codeModel.setHeight(400);
        codeModel.setFontSize(35);
        codeModel.setDesc(antiepidemicCommunity.getBuildingName());
        OutputStream outputStream = new BufferedOutputStream(response.getOutputStream());
        code.dealDesc(codeModel, outputStream);
    }
}